package lbframework.bean.colorChooser;

import java.awt.Color;
import javax.swing.JPopupMenu;
import lbframework.classe.Util;
import lbframework.classe.LBException;

public class LBColorChooser extends javax.swing.JPanel{

    public LBColorChooser(){
        initComponents();
        clcCor.setPreviewPanel(null);

    }

    public String getHexadecimal()throws Exception{
        Color c = new Color(clcCor.getColor().getRGB());
        return Util.formatNumber(Integer.toHexString( c.getRGB() & 0x00ffffff ), 6).toUpperCase();
    }

    public Color getRGB()throws Exception    {
        Color c = new Color(clcCor.getColor().getRGB());
        return c;
    }

    public int getDecimal()throws Exception{
        Color c = new Color(clcCor.getColor().getRGB());
        String hexa = Util.formatNumber(Integer.toHexString( c.getRGB() & 0x00ffffff ), 6).toUpperCase();

        return Integer.parseInt(hexa,16);
    }

    public void setHexadecimal(String i_valor)throws Exception {
        i_valor = Util.formatNumber(i_valor, 6);

        int r = Integer.parseInt(i_valor.substring(0, 2),16);
        int g = Integer.parseInt(i_valor.substring(2, 4),16);
        int b = Integer.parseInt(i_valor.substring(4, 6),16);

        clcCor.setColor(r, g, b);
        txtCor.setText(i_valor);
        lblCor.setBackground(clcCor.getColor());

    }

    public void setRGB(int i_r, int i_g, int i_b)throws Exception{
        clcCor.setColor(i_r, i_g, i_b);
        lblCor.setBackground(clcCor.getColor());

    }

    public void setDecimal(String i_valor)throws Exception{
        int i = Integer.parseInt(i_valor);
        String hexa = Integer.toHexString(i);
        txtCor.setText(hexa.toUpperCase());
        
        i_valor = Util.formatNumber(hexa, 6);

        int r = Integer.parseInt(i_valor.substring(0, 2),16);
        int g = Integer.parseInt(i_valor.substring(2, 4),16);
        int b = Integer.parseInt(i_valor.substring(4, 6),16);

        clcCor.setColor(r, g, b);
        lblCor.setBackground(clcCor.getColor());

    }

    @Override
    public void requestFocus(){
        btnCor.requestFocus();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popUp = new javax.swing.JPopupMenu();
        pnlPaleta = new lbframework.bean.panel.LBPanel();
        clcCor = new javax.swing.JColorChooser();
        btnOK = new lbframework.bean.button.LBButton();
        btnSair = new lbframework.bean.button.LBButton();
        btnCor = new lbframework.bean.button.LBButton();
        txtCor = new lbframework.bean.textField.LBTextField();
        frmFundo = new lbframework.bean.panel.LBPanel();
        lblCor = new lbframework.bean.label.LBLabel();

        pnlPaleta.setBorder(null);

        btnOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/ok.png"))); // NOI18N
        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/sair.png"))); // NOI18N
        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlPaletaLayout = new javax.swing.GroupLayout(pnlPaleta);
        pnlPaleta.setLayout(pnlPaletaLayout);
        pnlPaletaLayout.setHorizontalGroup(
            pnlPaletaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(clcCor, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlPaletaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        pnlPaletaLayout.setVerticalGroup(
            pnlPaletaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPaletaLayout.createSequentialGroup()
                .addComponent(clcCor, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlPaletaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnCor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/bean/colorChooser/color.png"))); // NOI18N
        btnCor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCorActionPerformed(evt);
            }
        });
        add(btnCor, new org.netbeans.lib.awtextra.AbsoluteConstraints(66, 0, 20, 20));

        txtCor.setBorder(null);
        txtCor.setEnabled(false);
        txtCor.setFocusable(false);
        add(txtCor, new org.netbeans.lib.awtextra.AbsoluteConstraints(2, 1, 39, 18));

        frmFundo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153)));
        frmFundo.setComponentPopupMenu(popUp);
        frmFundo.setFocusable(false);
        frmFundo.setInheritsPopupMenu(true);
        frmFundo.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblCor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblCor.setFocusable(false);
        lblCor.setOpaque(true);
        frmFundo.add(lblCor, new org.netbeans.lib.awtextra.AbsoluteConstraints(45, 5, 10, 10));

        add(frmFundo, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 60, 20));
    }// </editor-fold>//GEN-END:initComponents

    private void btnCorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCorActionPerformed
        try {
            int x = frmFundo.getX();
            int y = frmFundo.getY() + frmFundo.getHeight();

            popUp = new JPopupMenu();
            popUp.add(pnlPaleta);
            popUp.setSize(pnlPaleta.getWidth(), pnlPaleta.getHeight());
            popUp.show(this, x, y);

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
}//GEN-LAST:event_btnCorActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        try {
            popUp.setVisible(false);
        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
}//GEN-LAST:event_btnSairActionPerformed

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        try {
            popUp.setVisible(false);
            txtCor.setText(getHexadecimal());
            lblCor.setBackground(clcCor.getColor());

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_btnOKActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton btnCor;
    private lbframework.bean.button.LBButton btnOK;
    private lbframework.bean.button.LBButton btnSair;
    private javax.swing.JColorChooser clcCor;
    private lbframework.bean.panel.LBPanel frmFundo;
    private lbframework.bean.label.LBLabel lblCor;
    private lbframework.bean.panel.LBPanel pnlPaleta;
    private javax.swing.JPopupMenu popUp;
    private lbframework.bean.textField.LBTextField txtCor;
    // End of variables declaration//GEN-END:variables

}
